<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
</head>
<body>
    <br>
    <form class="layui-form layui-row layui-col-space16 layui-inline" style="margin-left: 20px">
        <div class="layui-inline">订单号</div>
        <div class="layui-inline" style="width: 220px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <input type="text" name="orderNo" value="" placeholder="请输入" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-inline">用户名</div>
        <div class="layui-inline" style="width: 150px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-form"></i>
                </div>
                <input type="text" name="userName" value="" placeholder="请输入" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-inline" style="width: 60px;">下单时间</div>
        <div class="layui-inline" style="width: 205px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-date"></i>
                </div>
                <input type="text" name="beginCreateTime" id="beginCreateTime" lay-verify="datetime" placeholder="开始时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="width: 205px;">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-date"></i>
                </div>
                <input type="text" name="endCreateTime" id="endCreateTime" lay-verify="datetime" placeholder="截止时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="submitSearch">Search</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="clear">Clear</button>
        </div>
    </form>
    <table class="layui-hide" id="test" lay-filter="test"></table>
<!--    头部工具条
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container layui-inline">
            <button class="layui-btn layui-btn-danger" lay-event="deleteAll">批量删除</button>
        </div>-->
<!--        搜索form表单

    </script>-->
<!--    右侧工具条-->
    <script type="text/html" id="barDemo">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="detail">详情</a>
        </div>
    </script>
    <script type="text/html" id="imageTemplet">
        <img src="{{d.mainImage}}">
    </script>
    <script type="text/html" id="sendTemplet">
        {{#     if (d.status == 20 || d.status == 40) {                }}
        <input type="checkbox" name="status" value="{{= d.orderNo }}" title="发货|未发货"
               lay-skin="switch" lay-filter="sendFilter" {{ d.status == 40 ? "checked" : "" }} >
        {{#     } else if (d.status == 50 || d.status == 60) {         }}
        已发货
        {{#     } else if (d.status == 0) {         }}
        不需发货
        {{#     } else {         }}
        暂不发货
        {{#     }                                   }}

    </script>
    <script type="text/html" id="statusTemplet">
        {{#     if (d.status == 0) {                }}
        <button type="button" class="layui-btn layui-bg-red layui-btn-sm">已取消</button>
        {{#     } else if (d.status == 10) {         }}
        <button type="button" class="layui-btn layui-bg-orange layui-btn-sm">未付款</button>
        {{#     } else if (d.status == 20) {         }}
        <button type="button" class="layui-btn layui-btn-sm">已付款</button>
        {{#     } else if (d.status == 40) {         }}
        <button type="button" class="layui-btn layui-bg-purple layui-btn-sm">已发货</button>
        {{#     } else if (d.status == 50) {         }}
        <button type="button" class="layui-btn layui-bg-blue layui-btn-sm">已收货</button>
        {{#     } else if (d.status == 60) {         }}
        <button type="button" class="layui-btn layui-btn-sm" style="background-color: chartreuse">已评论</button>
        {{#     }                                   }}
    </script>
    <script type="text/html" id="paymentTypeTemplet">
        {{#     if (d.paymentType == 0) {                }}
        货到付款
        {{#     } else if (d.paymentType == 1) {         }}
        微信支付
        {{#     } else if (d.paymentType == 2) {         }}
        支付宝
        {{#     } else if (d.paymentType == 3) {         }}
        银行卡
        {{#     }                                   }}
    </script>
    <script type="text/html" id="shippingTemplet">
        <ul>
            <li><strong>收货人:</strong> {{= d.receiverName }} </li>
            <li><strong>手机号码:</strong> {{= d.receiverMobile }} </li>
            <li>
                <strong>收货地址:</strong>
                {{= d.receiverProvince }}
                {{#     if (d.receiverCity === '市辖区'|| d.receiverCity === '县') {                }}
                {{#     } else {         }}
                {{= d.receiverCity }}
                {{#     }        }}
                {{= d.receiverArea }}
                {{= d.receiverAddress }}
            </li>
            <li><strong>邮政编码:</strong> {{= d.receiverZip }} </li>
        </ul>
    </script>
    <script type="text/html" id="isDeletedTemplet">
        <input type="checkbox" name="isDeleted" value="{{= d.orderNo }}" title="未删除|删除"
               lay-skin="switch" lay-filter="isDeletedFilter" {{ d.isDeleted == 0 ? "checked" : "" }}>
    </script>
    <script>
        layui.use(['table', 'form', 'laydate'], function () {
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;

            // 日期
            laydate.render({
                elem: '#beginCreateTime',
                type: 'datetime'
            });
            laydate.render({
                elem: '#endCreateTime',
                type: 'datetime'
            });
            $("#clear").click(function () {
                $("#secondCategotry").empty(); //会使选项清空
            })

            // 创建渲染实例
            table.render({
                elem: '#test',
                id: 'tableId',
                url:'/order/selectByPage',
                toolbar: '#toolbarDemo',
                page: true,
                cols: [
                        [
                            {type: 'checkbox', fixed: 'left'},
                            {field: 'orderNo', width:180, title: '订单号', sort: true},
                            {field: 'userName', title: '用户名', width: 80},
                            {field: 'shipping', title: '收货地址', width: 250, templet: '#shippingTemplet'},
                            {field: 'payment', title: '实付款金额', width: 110},
                            {field: 'status', title: '状态', templet: '#statusTemplet', align: 'center', width: 100},
                            {field: 'send', title: '发货', templet: '#sendTemplet', align: 'center', width: 100},
                            {field: 'isDeleted', title: '逻辑删除', templet: '#isDeletedTemplet', align: 'center', width: 100},
                            {field: 'paymentType', title: '付款方式', templet: '#paymentTypeTemplet', width: 90},
                            {field: 'discount', title: '折扣',  width: 90},
                            {field: 'postage', title: '运费', align: 'center', width: 60},
                            {field: 'paymentTime', title: '支付时间', width: 170},
                            {field: 'sendTime', title: '发货时间', width: 170},
                            {field: 'endTime', title: '收货时间', width: 170},
                            {field: 'closeTime', title: '评论时间', width: 170},
                            {field: 'createTime', title: '下单时间', width: 170},
                            {field: 'updateTime', title: '更新时间', width: 170},
                            {fixed: 'right', title:'操作', width: 80, align: 'center', minWidth: 125, toolbar: '#barDemo'}
                        ]
                ],
            });

            form.on('switch(sendFilter)', function(obj){
                //update blog set status=? where id=1;
                var orderNo = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var status = checked ? 40 : 20;
                $.post(
                    '/order/updateStatus',
                    {'orderNo': orderNo, 'status': status},
                    function (result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                            setInterval(function() {
                                //刷新页面
                                window.location.reload();
                            }, 2000)
                        }
                    },
                    'json'
                );
            });
            form.on('switch(isDeletedFilter)', function(obj){
                //update blog set status=? where id=1;
                var orderNo = this.value;
                var checked = obj.elem.checked;
                console.log(checked);
                var isDeleted = checked ? 0 : 1;
                $.post(
                    '/order/updateIsDeleted',
                    {'orderNo': orderNo, 'isDeleted': isDeleted},
                    function (result) {
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                        } else {
                            mylayer.errorMsg(result.msg);
                        }
                    },
                    'json'
                );
            });
            form.on('submit(submitSearch)', function(data){
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('tableId', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                });
                return false; // 阻止默认 form 跳转
            });
            table.on('toolbar(test)', function(obj){
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch(obj.event){
                    case 'LAYTABLE_TIPS':
                        layer.alert('自定义工具栏图标按钮');
                        break;
                };
            });
            table.on('tool(test)', function(obj){ // 双击 toolDouble
                var data = obj.data; // 获得当前行数据
                console.log(obj);
                if(obj.event === 'delete'){
                    layer.confirm(
                        '您确定要删除吗？',
                        {icon : 3},
                        function (index) {
                            $.post(
                                '/order/deleteById',
                                {'orderNo':data.orderNo},
                                function (result) {
                                    console.log(result);
                                    if (result.code == 0) {
                                        mylayer.okMsg(result.msg);
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'
                            );
                        }
                    );
                } else if (obj.event === 'detail'){
                    layer.open({
                        title: '订单详情',
                        type: 2,
                        area: ['800px', '450px'],// 宽高
                        content: '/page/order/detail?orderNo='+data.orderNo,
                    })
                }
            });
        });
    </script>
</body>
</html>
